package com.aaa.sys.controller;


import com.aaa.vo.R;
import com.aaa.sys.entity.Position;
import com.aaa.sys.service.DeptService;
import com.aaa.sys.service.PositionService;
import com.aaa.sys.vo.PositionVo;
import com.aaa.util.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 职位表 前端控制器
 * </p>
 *
 * @author ykq
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/system/position")
public class PositionController {

    @Autowired
    private PositionService positionService;

    @GetMapping("getAllPosition")
    public R getAllPosition(){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","ENABLE");
         return R.ok().data("allPosition",this.positionService.list(wrapper));
    }

    @PostMapping("findAllByCondition/{current}/{limit}")
    public R findAllByCondition(@PathVariable Integer current,@PathVariable Integer limit,@RequestBody PositionVo positionVo){
        Page<Position> page=this.positionService.findAllByCondition(current,limit,positionVo);
        return R.ok().data("records",page.getRecords()).data("total",page.getTotal());
    }

    @GetMapping("findById/{positionId}")
    public R findById(@PathVariable Long positionId){
        Position position = this.positionService.getById(positionId);
        return R.ok().data("position",position);
    }

    @PostMapping("/savePosition")
    public R savePosition(@RequestBody Position position, HttpServletRequest request){
        try{
            position.setCreateTime(new Date());
            position.setCreateUser(JwtUtils.getMemberIdByJwtToken(request.getHeader("token")));
            position.setUpdateTime(new Date());
            this.positionService.save(position);
            return R.ok().message("添加职位成功");
        }catch (Exception e){
            return R.error().message("添加职位失败");
        }
    }

    @PostMapping("updatePosition")
    public R updatePosition(@RequestBody Position position,HttpServletRequest request){
        try{
            position.setCreateUser(JwtUtils.getMemberIdByJwtToken(request.getHeader("token")));
            position.setUpdateTime(new Date());
            this.positionService.updateById(position);
            return R.ok().message("修改职位成功");
        }catch (Exception e){
            return R.error().message("修改职位失败");
        }
    }
    @PostMapping("updateStatus")
    public R updateStatus(@RequestParam("positionId")Long positionId,@RequestParam("status")String status,HttpServletRequest request){
        Position position=new Position();
        position.setPositionId(positionId);
        position.setStatus(status);
        position.setUpdateTime(new Date());
        position.setUpdateUser(JwtUtils.getMemberIdByJwtToken(request.getHeader("token")));
        this.positionService.updateById(position);
        return R.ok().message("状态修改成功");
    }
    @DeleteMapping("delete/{positionId}")
    public R delete(@PathVariable Long positionId){
        try{
            this.positionService.removeById(positionId);
            return R.ok().message("删除职位成功");
        }catch (Exception e){
            return R.error().message("删除职位失败");
        }
    }
}

